package com.sportizz.sport.row;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Instances;
import android.provider.CalendarContract.Reminders;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.facebook.Session;
import com.facebook.android.DialogError;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.widget.WebDialog;
import com.sportizz.model.Player;
import com.sportizz.sport.AlertDialogManager;
import com.sportizz.sport.DatabaseAdapter;
import com.sportizz.sport.Fb_dialog_box;
import com.sportizz.sport.MainActivity;
import com.sportizz.sport.R;
import com.sportizz.sport.Setting_Act;
import com.sportizz.sport.StartActivity;
import com.sportizz.sport.Utils;

public class old_ChampRow implements Row {

	private final Player player;
	private final LayoutInflater inflater;
	private DatabaseAdapter mDbHelper;

	Activity act_context;
	private boolean cpress = false;
	private String country_code;
	StartActivity sa = new StartActivity();

	String champ_channel1;
	String champ_channel2;
	String champ_channel3;
	String is_team = null;
	public ProgressDialog pd;
	AlertDialog.Builder alertDialog = null;
	AlertDialogManager alert = new AlertDialogManager();
	public boolean cal_btn = true;
	public Typeface font_champ;

	public old_ChampRow(LayoutInflater inflater, Player player, Context _context) {
		this.inflater = inflater;
		this.player = player;
		this.act_context = (Activity) _context;
	}

	@SuppressWarnings("static-access")
	@SuppressLint({ "NewApi", "SimpleDateFormat" })
	@Override
	public View getView(View convertView) {

		country_code = sa.GetCountryCode(act_context);
		final ViewHolder holder;
		View view;

		if (convertView == null) {
			ViewGroup viewGroup = (ViewGroup) inflater.inflate(
					R.layout.custom_row_champ, null);

			holder = new ViewHolder(
					(TextView) viewGroup.findViewById(R.id.txt_cdate),
					(TextView) viewGroup.findViewById(R.id.sport_c_name),
					(ImageView) viewGroup.findViewById(R.id.image_champ),
					(TextView) viewGroup.findViewById(R.id.txt_ch_name),
					(TextView) viewGroup.findViewById(R.id.txt_champ_game),
					(TextView) viewGroup.findViewById(R.id.txt_c_match),
					(TextView) viewGroup.findViewById(R.id.img_c_channel2),
					(TextView) viewGroup.findViewById(R.id.img_c_channel1),
					(TextView) viewGroup.findViewById(R.id.img_c_channel3),
					(Button) viewGroup.findViewById(R.id.btn_champ_follow),
					(Button) viewGroup.findViewById(R.id.btn_champ_addcal));

			viewGroup.setTag(holder);
			view = viewGroup;
		} else {
			holder = (ViewHolder) convertView.getTag();

			view = convertView;
		}

		Typeface sport_font = Typeface.createFromAsset(act_context.getAssets(),
				"hel57.ttf");
		
		holder.champ_follow.setTypeface(sport_font);
		holder.ibtn_addcal.setTypeface(sport_font);

		// holder.txt_champ_name.setTypeface(font_champ);

		holder.champ_follow.setTag(player.getChamp_code().toString());

		if (player.geti_foll() == true) {
			holder.champ_follow.setVisibility(View.INVISIBLE);
		}

		holder.champ_follow.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

//				Log.d("suraj", "player Name = " + StartActivity.player_name);

				mDbHelper = new DatabaseAdapter(act_context);
				mDbHelper.open();

				StartActivity.foll_count = StartActivity.follow_count.getInt(
						"follow_count", 0);

				Log.e("suraj", "StartActivity.foll_count = "
						+ StartActivity.foll_count);

//				if (StartActivity.foll_count < StartActivity.limited_follow
//						.getInt("max_follow", 3)
//						&& StartActivity.foll_count >= 0) {
					String btn_name = holder.champ_follow.getText().toString();

					if (btn_name.equals("FOLLOW") == true) {
						AsyncTask<Void, Void, Void> wait = new AsyncTask<Void, Void, Void>() {

							@Override
							protected void onPostExecute(
									Void result) {
								super.onPostExecute(result);
								pd.dismiss();
							
							}

							@Override
							protected void onPreExecute() {
								
								pd = ProgressDialog.show(
										act_context, "",
										"Following...", true);
								super.onPreExecute();
							}

							@Override
							protected Void doInBackground(
									Void... params) {
						try {
							
							final String device_id = sa
									.GetDeviceID(act_context);

							holder.txt_champ_name.setTag(player.getChamp_code());

							Log.d("suraj", "Team_id =" + player.getChamp_code());
							String response1 = null;

								
								
								mDbHelper.open();
								Long insert_c = mDbHelper.insert_follow_info(
										device_id, player.getChamp_code(),
										player.getChamp_name(),
										player.getsport_code(), player.getsport(),
										null, player.getteam());
								
								response1 = Utils
										.getJSONString("http://sportizza.com/interface/ios_follow.php?dev_id="
												+ device_id
												+ "&iso_cd="
												+ country_code
												+ "&follow=-1&follow_cs="
												+ player.getChamp_code() + "");

							

							

							Log.w("suraj", "insert_c = " + insert_c);
							mDbHelper.close();

							if (insert_c != 0 || response1 != null) {

								Thread t = new Thread(
										new Runnable() {
											public void run() {

												act_context.runOnUiThread(new Runnable() {
													public void run() {
														
								holder.champ_follow.setText("UNFOLLOW");
								
								MainActivity.playerAll.add(player
										.getChamp_code());

								Toast.makeText(
										act_context.getApplicationContext(),
										"Added to I follow", Toast.LENGTH_SHORT)
										.show();
								// holder.progressBar_follow.setVisibility(View.INVISIBLE);

								Log.d("suraj", "follow success..");

								StartActivity.foll_count++;
								//
								SharedPreferences.Editor editor = StartActivity.follow_count
										.edit();
								editor.putInt("follow_count",
										StartActivity.foll_count);
								editor.commit();
													}
												});
											}
										});
								t.start();

							} else {
								Toast.makeText(
										act_context.getApplicationContext(),
										"Error during follow",
										Toast.LENGTH_SHORT).show();
							}

							cpress = true;
							mDbHelper.close();

						} catch (Exception e) {
							e.printStackTrace();
							Toast.makeText(act_context.getApplicationContext(),
									"Error in follow ..", Toast.LENGTH_SHORT)
									.show();

							mDbHelper.close();

							// holder.progressBar_follow.setVisibility(View.INVISIBLE);
						}
						return null;
							}
						};
						wait.execute();
						
					} else if (cpress == true) {
						Log.d("suraj", "Already follow");
					}

			}
		});

		holder.ibtn_addcal.setTag(player.getDate1().toString());

		holder.ibtn_addcal.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
//				if (MainActivity.check_cal.isChecked() == true) {
					String date_str = (String) holder.ibtn_addcal.getTag();

					Log.v("suraj", "DAte = " + date_str);

					Long start_date_mili = null;

					String title = player.getgametext() + " - "
							+ player.getChamp_name();

					SimpleDateFormat dateFormat11 = new SimpleDateFormat(
							"dd-MM-yyyy hh:mm a");

					Date date11 = null;

					try {
						date11 = dateFormat11.parse(date_str);
					} catch (ParseException e) {
						e.printStackTrace();
					}

					start_date_mili = date11.getTime();

					long begin = start_date_mili;
					long end = start_date_mili + 30 * 60 * 1000;// ending time
																// in
																// milliseconds
					String[] proj = new String[] { Instances._ID,
							Instances.BEGIN, Instances.END, Instances.EVENT_ID };

					Cursor cursor = Instances.query(
							act_context.getContentResolver(), proj, begin, end);

					cursor.moveToFirst();

					Log.v("suraj", "Cursor = " + cursor);

					Log.v("suraj", "Cursor COUNT = " + cursor.getCount());

					if (cursor.getCount() > 0) {
						String title_cal = cursor.getString(cursor
								.getColumnIndex("title"));

						if (title_cal.equals(title)) {
							Log.i("suraj",
									"Cursor = "
											+ cursor.getString(cursor
													.getColumnIndex("title")));

							Toast.makeText(act_context,
									"Already Added to Calendar..",
									Toast.LENGTH_SHORT).show();
						} else {
							Log.i("suraj",
									"Setting_Act.check_cal.isChecked() = "
											+ Setting_Act.check_cal.isChecked());

							Log.i("suraj", "Add to calendar = " + title);

							addToCalendar(act_context, player.getChamp_name(),
									player.getgametext(), title,
									start_date_mili,
									start_date_mili + 30 * 60 * 1000);
							Toast.makeText(act_context, "Added to Calendar..",
									Toast.LENGTH_SHORT).show();

						}
					} else {
						Log.i("suraj", "Setting_Act.check_cal.isChecked() = "
								+ Setting_Act.check_cal.isChecked());

						Log.i("suraj", "Add to calendar = " + title);

						addToCalendar(act_context, player.getChamp_name(),
								player.getgametext(), title, start_date_mili,
								start_date_mili + 30 * 60 * 1000);
						Toast.makeText(act_context, "Added to Calendar..",
								Toast.LENGTH_SHORT).show();
					}
//				} else {
//					Toast.makeText(act_context,
//							"Access Calendar is off in setting",
//							Toast.LENGTH_SHORT).show();
//				}
			}

		});

//		holder.btn_fbshare_ch.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				
//				Log.e("suraj","Session =="+ Session.getActiveSession().isOpened());
//				
//				if(Session.getActiveSession().isOpened())
//				{
//					postToWall(player.getChamp_name(),player.getgametext(),player.getDate());
//				}
//				else
//				{
//					Fb_dialog_box pdb = new Fb_dialog_box(act_context);
//					pdb.show();
//				}
//				
////				String access_token1 = StartActivity.mPrefs.getString("access_token", null);
////				if (access_token1 != null) {
////					StartActivity.facebook.setAccessToken(access_token1);
////					postToWall(player.getChamp_name(),player.getgametext(),player.getDate());
////					Log.d("FB Sessions", "" + StartActivity.facebook.isSessionValid());
////				} else 
////				{
////					loginToFacebook();
////					
////				}
//				
//			}
//		});
		
		
		String champ_img_url = null;

		InputStream is1 = null;
		try {
			if (player.gettablet() == false) {
				is1 = act_context
						.getResources()
						.getAssets()
						.open("default_images/phone/" + player.getsport_code()
								+ ".png");
				Bitmap res1 = BitmapFactory.decodeStream(is1);

				Log.i("suraj", "champ =" + res1);

				Bitmap output1 = Bitmap.createBitmap(res1.getWidth(),
						res1.getHeight(), Bitmap.Config.ARGB_8888);
				Canvas canvas1 = new Canvas(output1);

				Paint paint = new Paint();
				Rect rect = new Rect(0, 0, res1.getWidth(), res1.getHeight());
				RectF rectF = new RectF(rect);
				float roundPx1 = 8f;

				paint.setAntiAlias(true);
				canvas1.drawARGB(0, 0, 0, 0);
				paint.setColor(0xff424242);
				canvas1.drawRoundRect(rectF, roundPx1, roundPx1, paint);
				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas1.drawBitmap(res1, rect, rect, paint);

				holder.champ_image.setImageBitmap(output1);
				holder.champ_image.setScaleType(ScaleType.FIT_CENTER);

				champ_img_url = "" + StartActivity.get_img_location
						+ "phone/CS/" + player.getChamp_code()
						+ ".png";
				
				new imageTask(holder.champ_image).execute(champ_img_url);

			} else if (player.gettablet() == true) {
				is1 = act_context
						.getResources()
						.getAssets()
						.open("default_images/phone/" + player.getsport_code()
								+ "@2x.png");

				Bitmap res2 = BitmapFactory.decodeStream(is1);

				Log.i("suraj", "" + res2);

				Bitmap output1 = Bitmap.createBitmap(res2.getWidth(),
						res2.getHeight(), Bitmap.Config.ARGB_8888);
				Canvas canvas1 = new Canvas(output1);

				Paint paint = new Paint();
				Rect rect = new Rect(0, 0, res2.getWidth(), res2.getHeight());
				RectF rectF = new RectF(rect);
				float roundPx1 = 8f;

				paint.setAntiAlias(true);
				canvas1.drawARGB(0, 0, 0, 0);
				paint.setColor(0xff424242);
				canvas1.drawRoundRect(rectF, roundPx1, roundPx1, paint);
				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas1.drawBitmap(res2, rect, rect, paint);

				holder.champ_image.setImageBitmap(output1);
				holder.champ_image.setScaleType(ScaleType.FIT_CENTER);

				champ_img_url = "" + StartActivity.get_img_location
						+ "phone/CS/" + player.getChamp_code()
						+ ".png";
				new imageTask(holder.champ_image).execute(champ_img_url);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		holder.txt_champ_name.setText(player.getChamp_name());
		holder.txt_champ_name
				.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
		//
		holder.txt_champ_date.setText(" " + player.getmydate_cs());
		// holder.txt_champ_date.setTextColor(Color.WHITE);

		

		holder.sport_name.setText(player.getsport());
		holder.sport_name.setTypeface(sport_font);
		holder.sport_name.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

		Log.d("suraj", "playerAll = " + MainActivity.playerAll);

		Log.d("suraj",
				"follow_btn1.getTag() = "
						+ MainActivity.playerAll.contains(holder.champ_follow
								.getTag().toString()) + "=="
						+ holder.champ_follow.getTag().toString());

		if (MainActivity.playerAll != null) {
			if (MainActivity.playerAll.contains(holder.champ_follow.getTag()
					.toString()) == true) {
				holder.champ_follow.setText("UNFOLLOW");
			} else {
				holder.champ_follow.setText("FOLLOW");
			}
		}

		Log.v("suraj", "Match Text = " + player.getgametext());

		if (player.getgametext().equalsIgnoreCase("null")
				|| player.getgametext() == null) {

		} else {
			holder.txt_gametext.setText(player.getgametext());
			holder.txt_gametext
					.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
		}

		if (player.getmatchtext().equalsIgnoreCase("null")
				|| player.getmatchtext() == null) {

		} else {
			holder.txt_c_match.setText(player.getmytime_cs());
			holder.txt_c_match
					.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
		}

		if (player.getchannel_code1().equals("null")
				|| player.getchannel_code1().equals("NONE")
				|| player.getchannel_code1() == null) {
			holder.channel1.setVisibility(View.INVISIBLE);
		} else {
			holder.channel1.setVisibility(View.VISIBLE);
		}

		if (player.getchannel_code2().equals("null")
				|| player.getchannel_code2().equals("NONE")
				|| player.getchannel_code2() == null) {
			holder.channel2.setVisibility(View.INVISIBLE);
		} else {
			holder.channel2.setVisibility(View.VISIBLE);
		}

		if (player.getchannel_code3().equals("null")
				|| player.getchannel_code3().equals("NONE")
				|| player.getchannel_code3() == null) {
			holder.channel3.setVisibility(View.INVISIBLE);
		} else {
			holder.channel3.setVisibility(View.VISIBLE);
		}


		holder.channel1.setTypeface(sport_font);
		holder.channel2.setTypeface(sport_font);
		holder.channel3.setTypeface(sport_font);
		
		holder.channel1.setText(player.getchannel_code1());
		holder.channel2.setText(player.getchannel_code2());
		holder.channel3.setText(player.getchannel_code3());

		return view;

	}

	
/*
//	protected void loginToFacebook() {
//		  
//		  String access_token = StartActivity.mPrefs.getString("access_token", null);
//		  long expires = StartActivity.mPrefs.getLong("access_expires", 0);
//		 
//		  if (access_token != null) {
//			  StartActivity.facebook.setAccessToken(access_token);
//		 
//			  Log.d("FB Sessions", "" + StartActivity.facebook.isSessionValid());
//		  }
//		 
//		  if (expires != 0) {
//			  StartActivity.facebook.setAccessExpires(expires);
//		  }
//		 
//		  if (!StartActivity.facebook.isSessionValid()) {
//			  StartActivity.facebook.authorize(act_context,
//		     new String[] { "email", "publish_stream" },
//		     new DialogListener() {
//		 
//		      @Override
//		      public void onCancel() {
//		      }
//		 
//		      @Override
//		      public void onComplete(Bundle values) {
//		       SharedPreferences.Editor editor = StartActivity.mPrefs.edit();
//		       editor.putString("access_token",
//		    		   StartActivity.facebook.getAccessToken());
//		       editor.putLong("access_expires",
//		    		   StartActivity.facebook.getAccessExpires());
//		       editor.commit();
//		 
//		       postToWall(player.getChamp_name(),player.getsport(),player.getDate());
//		      }
//		 
//		      @Override
//		      public void onError(DialogError error) {
//		 
//		      }
//		 
//		      @Override
//		      public void onFacebookError(FacebookError fberror) {
//		 
//		      }
//		 
//		     });
//		  }
//	}
*/

	protected void postToWall(String champ_name, String game, String date) {
		
		String[] date_only=date.split(" ");
		
		 final Bundle postParams = new Bundle();
	     postParams.putString("name", champ_name);
	     postParams.putString("caption", game);
	     postParams.putString("description", date_only[0]);
	     postParams.putString("link", "https://play.google.com/store/apps/details?id=com.sportizz.sport&hl=en");
	     postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

	     act_context.runOnUiThread(new Runnable()
		    {
		        @Override
		        public void run() 
		        {
		            WebDialog feedDialog = new WebDialog.FeedDialogBuilder(act_context, Session.getActiveSession(), postParams).build();
		            feedDialog.show();
		        }
		    });
	 	
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	public void addToCalendar(Context ctx, final String title,
			final String desc, final String tit, final long dtstart,
			final long dtend) {

		Uri eventsUri, reminderuri;

		if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 14) {
			eventsUri = Uri.parse("content://com.android.calendar/events");
			reminderuri = Uri.parse("content://com.android.calendar/reminders");
			// //
			ContentValues event = new ContentValues();

			event.put(Events.CALENDAR_ID, 1);
			event.put(Events.TITLE, tit);
			event.put(Events.DESCRIPTION, "SPORTIZZA");
			event.put(Events.DTSTART, dtstart);
			event.put(Events.DTEND, dtend);
			event.put(Events.STATUS, 1);
			event.put(Events.EVENT_COLOR,Color.GREEN);
			
			event.put(Events.HAS_ALARM, 1);
			event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone
					.getDefault().getID());

			// To Insert
			Log.v("suraj", "Event =" + TimeZone.getDefault().getID());

			Uri uri = ctx.getContentResolver().insert(
					CalendarContract.Events.CONTENT_URI, event);

			long eventID = Long.parseLong(uri.getLastPathSegment());

			ContentValues reminders = new ContentValues();
			reminders.put(Reminders.EVENT_ID, eventID);
			reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
			reminders.put(Reminders.MINUTES, 15);

			ctx.getContentResolver().insert(
					CalendarContract.Reminders.CONTENT_URI, reminders);

			Log.v("suraj", "Reminder =" + reminders);

		} else if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 8
				&& Integer.parseInt(android.os.Build.VERSION.SDK) < 14) {
			//
			eventsUri = Uri.parse("content://com.android.calendar/events");
			reminderuri = Uri.parse("content://com.android.calendar/reminders");
			// //

			ContentValues event = new ContentValues();

			event.put(Events.CALENDAR_ID, 1);
			event.put(Events.TITLE, tit);
			event.put(Events.DESCRIPTION, "SPORTIZZA");
			event.put(Events.DTSTART, dtstart);
			event.put(Events.DTEND, dtend);
			event.put(Events.STATUS, 1);
			event.put(Events.HAS_ALARM, 1);
			event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone
					.getDefault().getID());

			// To Insert
			Log.v("suraj", "Event =" + event);

			Uri uri = ctx.getContentResolver().insert(eventsUri, event);

			long eventID = Long.parseLong(uri.getLastPathSegment());

			ContentValues reminders = new ContentValues();
			reminders.put(Reminders.EVENT_ID, eventID);
			reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
			reminders.put(Reminders.MINUTES, 15);

			ctx.getContentResolver().insert(reminderuri, reminders);

			Log.v("suraj", "Reminder =" + reminders);
		}

	}

	@Override
	public int getViewType() {
		return Rowtype.CHAMP_ROW.ordinal();
	}

	private static class ViewHolder {
		final ImageView champ_image;
		final TextView txt_champ_name;
		final TextView sport_name;
		final TextView txt_champ_date;
		final TextView txt_c_match;
		final TextView txt_gametext;
		final TextView channel1;
		final TextView channel2;
		final TextView channel3;

		final Button champ_follow;
		final Button ibtn_addcal;

		private ViewHolder(TextView champ_date, TextView sport_c_name,
				ImageView champ_image,
				TextView txt_champ_name, TextView champ_game,
				TextView txt_c_match, TextView channel1, TextView channel2,
				TextView channel3, Button champ_follow,Button ibtn_addcal) {
			
			this.champ_image = champ_image;
			this.txt_champ_name = txt_champ_name;
			this.sport_name = sport_c_name;
			this.txt_champ_date = champ_date;
			this.txt_c_match = txt_c_match;
			this.txt_gametext = champ_game;

			this.channel1 = channel1;
			this.channel2 = channel2;
			this.channel3 = channel3;
			this.champ_follow = champ_follow;
			this.ibtn_addcal = ibtn_addcal;
//			this.btn_fbshare_ch = btn_fbshare_ch;
		}

	}

	private class imageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView imageView = null;

		imageTask(ImageView iv) {
			this.imageView = iv;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			try {
				return download_Image(params[0]);

			} catch (OutOfMemoryError e) {
				return null;
			}

		}

		@Override
		protected void onPostExecute(Bitmap result) {
			Bitmap output;
			Canvas canvas;
			float roundPx;

			if (result == null) {

				InputStream is = null;

				try {
					if (player.gettablet() == false) {
						is = act_context
								.getResources()
								.getAssets()
								.open("default_images/phone/"
										+ player.getsport_code() + ".png");
					} else if (player.gettablet() == true) {
						is = act_context
								.getResources()
								.getAssets()
								.open("default_images/phone/"
										+ player.getsport_code() + "@2x.png");
					}

					result = BitmapFactory.decodeStream(is);

				} catch (Exception e) {
					result = BitmapFactory.decodeResource(
							act_context.getResources(), R.drawable.ic_launcher);
				}

				output = Bitmap.createBitmap(result.getWidth(),
						result.getHeight(), Bitmap.Config.ARGB_8888);
				canvas = new Canvas(output);

				Paint paint = new Paint();
				Rect rect = new Rect(0, 0, result.getWidth(),
						result.getHeight());
				RectF rectF = new RectF(rect);
				roundPx = 8f;

				paint.setAntiAlias(true);
				canvas.drawARGB(0, 0, 0, 0);
				paint.setColor(0xff424242);
				canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas.drawBitmap(result, rect, rect, paint);

				imageView.setImageBitmap(output);
				imageView.setScaleType(ScaleType.FIT_CENTER);

			} else {
				output = Bitmap.createBitmap(result.getWidth(),
						result.getHeight(), Bitmap.Config.ARGB_8888);
				canvas = new Canvas(output);

				Paint paint = new Paint();
				Rect rect = new Rect(0, 0, result.getWidth(),
						result.getHeight());
				RectF rectF = new RectF(rect);
				roundPx = 8f;

				paint.setAntiAlias(true);
				canvas.drawARGB(0, 0, 0, 0);
				paint.setColor(0xff424242);
				canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
				paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
				canvas.drawBitmap(result, rect, rect, paint);

				imageView.setImageBitmap(output);

			}
			super.onPostExecute(result);
		}

		private Bitmap download_Image(String url) {
			// ---------------------------------------------------
			Bitmap bm = null;
			try {
				URL aURL = new URL(url);
				URLConnection conn = aURL.openConnection();
				conn.connect();
				InputStream is = conn.getInputStream();
				BufferedInputStream bis = new BufferedInputStream(is);
				bm = BitmapFactory.decodeStream(bis);
				bis.close();
				is.close();
			} catch (Exception e) {
				Log.e("Hub", "Error getting the image from server : ");
				InputStream iss = null;
				try {
					iss = act_context
							.getResources()
							.getAssets()
							.open("default_images/phone/"
									+ player.getsport_code() + ".png");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				return BitmapFactory.decodeStream(iss);
			}

			return bm;

		}

	}

	// private class imageTask_channel extends AsyncTask<String, Void, Bitmap> {
	// ImageView imageView = null;
	//
	// public imageTask_channel(ImageView channel) {
	// this.imageView = channel;
	//
	// }
	//
	//
	//
	// @Override
	// protected void onPostExecute(Bitmap result) {
	// if (result == null) {
	// imageView.setVisibility(View.INVISIBLE);
	// } else {
	// imageView.setImageBitmap(result);
	// imageView.setScaleType(ScaleType.FIT_XY);
	// }
	// super.onPostExecute(result);
	// }
	//
	// @Override
	// protected Bitmap doInBackground(String... params) {
	// try {
	// return download_Image(params[0]);
	//
	// } catch (OutOfMemoryError e) {
	// return null;
	// }
	// }
	//
	//
	//
	// private Bitmap download_Image(String url) {
	// Bitmap bm = null;
	// try {
	// URL aURL = new URL(url);
	// URLConnection conn = aURL.openConnection();
	// conn.connect();
	// InputStream is = conn.getInputStream();
	// BufferedInputStream bis = new BufferedInputStream(is);
	// bm = BitmapFactory.decodeStream(bis);
	// bis.close();
	// is.close();
	// } catch (Exception e) {
	// Log.e("Hub", "Error getting the image from server : ");
	// }
	// return bm;
	//
	// }
	// }
}
